<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_vpage_system_attribute.class.php');


class TCMS_VPage_System_Attribute_DBC extends TCMS_VPage_System_Attribute implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select vpage, value, t_sa_id from tcms_vpage_system_attributes where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_vpage($rez_obj->VPAGE);
			$this->set_value(stripslashes($rez_obj->VALUE));
			$this->set_t_sa_id($rez_obj->T_SA_ID);

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_vpage_system_attributes_seq');

		$vpage = $this->get_vpage();
		$value = addslashes($this->get_value());
		$t_sa_id = $this->get_t_sa_id();


		if ($id) {
			$sql = "insert into tcms_vpage_system_attributes (id, ".
									      "vpage, ".
									      "value, ".
									      "t_sa_id ".
									      ") ".
								"values ".
									      "($id,".
									      "$vpage, ".
									      "'$value', ".
									      "$t_sa_id ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_vpage_system_attributes_seq');
		}

		return $ret;
	}



	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$vpage = $this->get_vpage();
		$value = addslashes($this->get_value());
		$t_sa_id = $this->get_t_sa_id();

		$sql = "update tcms_vpage_system_attributes set ".
						"vpage = $vpage, ".
						"value = '$value', ".
						"t_sa_id = $t_sa_id ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_vpage_system_attributes where id = $id";
		$dbc->execute($sql);
	}


	public static function load_all_for_vpage(DB_Connection $dbc, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);

		$ret = array();
		$sql = "select id from tcms_vpage_system_attributes where vpage = $vpage_id";
		$rez = $dbc->execute($sql);
		while ($rez_obj = $rez->fetch_object()) {
			$tmp = new TCMS_VPage_System_Attribute_DBC();
			$tmp->load_by_id($dbc, $rez_obj->ID);

			$ret[$tmp->get_t_sa_id()] = clone $tmp;
		}

		return $ret;
	}


	public static function delete_for_vpage(DB_Connection $dbc, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);

		$sql = "delete from tcms_vpage_system_attributes where vpage = $vpage_id";
		$rez = $dbc->execute($sql);
	}


	/**
	 * Loads TCMS_VPage_System_Attribute_DBC by TCMS_VPT_System_Attribute_DBC's HID and vpage id
	 * @param DB_Connection $dbc
	 * @param String $hid Attribute HID
	 * @param Integer $vpage_id VPage's ID
	 * @return TCMS_VPage_System_Attribute_DBC
	 */
	public static function load_by_hid_and_vpage(DB_Connection $dbc, $hid, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);
		$hid = addslashes($hid);

		$ret = false;

		$sql = "select sa.id from tcms_vpage_system_attributes sa, tcms_vpt_system_attributes tsa where sa.vpage =  $vpage_id and tsa.sa_export_name = '$hid' and sa.t_sa_id = tsa.id";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$tmp = new TCMS_VPage_System_Attribute_DBC();
			$tmp->load_by_id($dbc, $rez_obj->ID);
			$ret = $tmp;
		}

		return $ret;
	}



	/**
	 * @param DB_Connection $dbc
	 * @param String $sa_export_name HID / Export name of the system attribute
	 * @param Integer $vpage_id VPage's ID
	 * @return Array Returns array of objects of type TCMS_VPage_System_Attribute_DBC
	 * @deprecated Use load_by_hid_and_vpage() instead
	 */
	public static function load_all_by_sa_export_name_vpage(DB_Connection $dbc, $sa_export_name, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);
		$sa_export_name = addslashes($sa_export_name);

		$sql = "select sa.id from tcms_vpage_system_attributes sa, tcms_vpt_system_attributes tsa where sa.vpage =  $vpage_id and tsa.sa_export_name = '$sa_export_name' and sa.t_sa_id = tsa.id";
		$rez = $dbc->execute($sql);
		$ret = array();
		while ($rez_obj = $rez->fetch_object()) {
			$tmp = new TCMS_VPage_System_Attribute_DBC();
			$tmp->load_by_id($dbc, $rez_obj->ID);
			$ret[] = clone $tmp;
		}

		return $ret;
	}


	/**
	 * Alias of load_all_by_sa_export_name_vpage();
	 *
	 * @param DB_Connection $dbc
	 * @param String $sa_export_name HID / Export name of the system attribute
	 * @param Integer $vpage_id VPage's ID
	 * @return Array Returns array of objects of type TCMS_VPage_System_Attribute_DBC
	 * @deprecated Use load_by_hid_and_vpage() instead
	 */

	public static function load_all_by_hid_vpage(DB_Connection $dbc, $sa_export_name, $vpage_id) {
		return TCMS_VPage_System_Attribute_DBC::load_all_by_sa_export_name_vpage($dbc, $sa_export_name, $vpage_id);
	}


	public static function count_for_vpt_sa(DB_Connection $dbc, $vpt_sa_id) {
		tangra_if_not_int_throw_e($vpt_sa_id);
		$sql = "select count(id) as c from tcms_vpage_system_attributes where t_sa_id = $vpt_sa_id";
		$rez = $dbc->execute($sql);
		$rez_obj = $rez->fetch_object();

		return $rez_obj->C;
	}


	public static function delete_all_for_vpt_sa(DB_Connection $dbc, $vpt_sa_id) {
		tangra_if_not_int_throw_e($vpt_sa_id);
		$sql = "delete from tcms_vpage_system_attributes where t_sa_id = $vpt_sa_id";
		$dbc->execute($sql);
	}
}